
import ParserError from "@/js/GraphParser/src/ParserError"
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import * as InputLinkNode from "@/js/components/InputLinkNode"

export class TryCatch extends GraphNode {
    layout?: DefaultLayout
    load() {
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView)
        this.layout = layout
        this.appendChild(layout.container)
        
        const nodes: (InputLinkNodeOptions & { layout: 'left' | 'right', inputLinkNode: InputLinkNodes })[] = [
            {
                name: "tryBody",
                desc: '执行',
                model: "output",
                showInput: false,
                layout: 'right',
                inputLinkNode: "InputLinkNodeFlow"
            },
            {
                name: "catchBody",
                desc: '异常',
                model: "output",
                showInput: false,
                layout: 'right',
                inputLinkNode: "InputLinkNodeFlow"
            },
        ]

        nodes.forEach(node => {
            if (layout.main) {
                const layout_ = layout.main[node.layout]
                if (layout_) layout_.appendChild(
                    new (InputLinkNode as any)[node.inputLinkNode](this, this.graphView, node)
                )
            }

        })
    }
}

TryCatch.parser = ({node, next}) => {
    const output = next(node.outputs.output)
    const tryBody = next(node.outputs.tryBody)
    const catchBody = next(node.outputs.catchBody)
    
    return `try { ${tryBody} } catch (error) {${catchBody}} ${output}`
}